package auctions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;

public class SealedBidAuction extends Auction {

	public SealedBidAuction(int auctionNumber, String outputFile, int numBidders, Double reserve, int paymentRule) {
		super(auctionNumber, outputFile, numBidders, reserve, paymentRule);
	}
	
	@Override
	public Vector<AuctionResults> getWinner() {
		/**Vector<BidderResults> bresults = new Vector<BidderResults>();
		for (int i=0; i<_agents.size(); i++) {
			bresults.add(new BidderResults(_agents.get(i)));
		}
		**/
		
		
		//Accumulate bids
		double maxBid = 0;
		int maxBidder = 0;
		Vector<Double> bids = new Vector<Double>();  //This could be extended
		for (int agent = 0; agent<_agents.size(); agent++) {
			bids.add(_agents.get(agent).getBid(_auctionNumber));
			if (bids.get(agent) > maxBid) {
				 maxBid = bids.get(agent);
				 maxBidder = agent;
			 }
		}
			
		//Now determine the winning price
		ArrayList<Double> sortedBids = new ArrayList<Double>(bids);
		Collections.sort(sortedBids);
			
		int priceIndex = sortedBids.size() - _paymentRule;
		double price;
		if (priceIndex < 0)  { 
                    //TODO could reserve price be higher than any bids?
                    price = _reserve;
		} else { 
                    price = sortedBids.get(priceIndex);
		}
			
		_agents.get(maxBidder).addWinnings(_auctionNumber, price);
		//bresults.get(maxBidder).addWinning(i, price);
        Vector<AuctionResults> ar = new Vector<AuctionResults>();
        ar.add(new AuctionResults(price, bids, maxBidder, _auctionNumber));
        _ars = ar;
		return ar;
		//AuctionsToXML writer = new AuctionsToXML(results, bresults);
		//writer.generateXML(_outputFile);
        
	}

	@Override
	public AuctionType getType() {
		return AuctionType.SEALEDBID;
	}

}
